package pers.ljf.escloud.controller.sys;

import org.springframework.web.bind.annotation.*;
import pers.ljf.escloud.annotation.SysLog;
import pers.ljf.escloud.common.req.UserSaveReq;
import pers.ljf.escloud.dao.entity.app.User;
import pers.ljf.escloud.service.dto.Result;
import pers.ljf.escloud.service.modules.app.UserService;
import pers.ljf.escloud.service.util.PageUtils;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/sys/appUser")
public class SysAppUserController extends AbstractController {
    @Resource
    private UserService userService;

    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(getUser().getEntId(), params);

        return Result.ok().put("page", page);
    }

    /**
     * 删除用户
     */
    @SysLog("删除前台用户")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        userService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    @GetMapping("/info/{userId}")
    public Result info(@PathVariable("userId") Long menuId){
        User user = userService.getById(menuId);
        return Result.ok().put("user", user);
    }

    @PostMapping("/save")
    public Result save(@RequestBody @Valid UserSaveReq req) {
        userService.save(req);
        return Result.ok("更新成功");
    }
}
